use std::{env, fs, process};

#[cfg(feature = "logging")]
fn init_logging() {
    let _ = env_logger::try_init();
}

#[cfg(not(feature = "logging"))]
fn init_logging() {}

fn main() {
    init_logging();
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }

    let path = &args[1];

    let src = match fs::read_to_string(path) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Failed to read '{}': {}", path, e);
            process::exit(1);
        }
    };

    match compiler::generate_mir(&src) {
        Ok(out) => {
            print!("{}", out);
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    }
}
